
package com.phevos.web.cas.client;

import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

public final class Util
{

    /**
     * Returns a service ID (URL) as a composite of the preconfigured server name and the runtime request.
     */
    public static String getService(HttpServletRequest request) throws ServletException
    {
        String addr = request.getLocalAddr();
        int port = request.getLocalPort();
        // now, construct our best guess at the string
        StringBuffer sb = new StringBuffer();
        if (request.isSecure())
        {
            sb.append("https://");
        }
        else
        {
            sb.append("http://");
        }
        sb.append(addr);
        sb.append(':');
        sb.append(port);
        sb.append(request.getRequestURI());

        if (request.getQueryString() != null)
        {
            // first, see whether we've got a 'ticket' at all
            int ticketLoc = request.getQueryString().indexOf("ticket=");

            // if ticketLoc == 0, then it's the only parameter and we ignore
            // the whole query string

            // if no ticket is present, we use the query string wholesale
            if (ticketLoc == -1)
            {
                sb.append("?" + request.getQueryString());
            }
            else if (ticketLoc > 0)
            {
                ticketLoc = request.getQueryString().indexOf("&ticket=");
                if (ticketLoc == -1)
                {
                    // there was a 'ticket=' unrelated to a parameter named
                    // 'ticket'
                    sb.append("?" + request.getQueryString());
                }
                else if (ticketLoc > 0)
                {
                    // otherwise, we use the query string up to "&ticket="
                    sb.append("?" + request.getQueryString().substring(0, ticketLoc));
                }
            }
        }
        return URLEncoder.encode(sb.toString());
    }

    public static String getRedirect(HttpServletRequest request) throws ServletException
    {
        String addr = request.getLocalAddr();
        int port = request.getLocalPort();
        // now, construct our best guess at the string
        StringBuffer sb = new StringBuffer();
        sb.append(request.getRequestURI());
        if (request.getQueryString() != null)
        {
            // first, see whether we've got a 'ticket' at all
            int ticketLoc = request.getQueryString().indexOf("ticket=");

            // if ticketLoc == 0, then it's the only parameter and we ignore
            // the whole query string

            // if no ticket is present, we use the query string wholesale
            if (ticketLoc == -1)
            {
                sb.append("?" + request.getQueryString());
            }
            else if (ticketLoc > 0)
            {
                ticketLoc = request.getQueryString().indexOf("&ticket=");
                if (ticketLoc == -1)
                {
                    // there was a 'ticket=' unrelated to a parameter named
                    // 'ticket'
                    sb.append("?" + request.getQueryString());
                }
                else if (ticketLoc > 0)
                {
                    // otherwise, we use the query string up to "&ticket="
                    sb.append("?" + request.getQueryString().substring(0, ticketLoc));
                }
            }
        }
        return sb.toString();
    }
}
